// packageF/others/wisdom/addWisdomBank/addWisdomBank.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: "",
    card: "",
    card_type: "",
    identity: "",
    mobile: "",
    cvv2: "",
    valid_day: "",
    cardShow: false,
    radioCard: "",
    bankCard: [{
        name: "储蓄卡",
        id: 1
      },
      {
        name: "信用卡",
        id: 2
      }
    ],
    // 有效期选择
    timeShow: false,
    maxDate: new Date().getTime(),
    currentDate: new Date().getTime(),
    // 跳转标识
    to: '',
    option: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: '添加银行卡',
    })
    if (options.to) {
      this.data.to = options.to;
    }
    if (options.option) {
      this.data.option = option.option;
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  cardShowClose() {
    this.setData({
      cardShow: false
    })
  },
  childParams(e) {
    let type = e.currentTarget.dataset.key;
    if (type == "card_type") {
      this.setData({
        cardShow: true
      })
    } else if (type == "valid_day") {
      // 有效期
      this.setData({
        timeShow: true
      })
    }
  },
  onClickRight_close(e) {
    this.setData({
      timeShow: false
    })
  },
  onClickRight_cardShow(e) {
    //银行卡类型
    this.setData({
      card_type: this.data.radioCard == "储蓄卡" ? 1 : 2,
      cardShow: false
    })
  },
  // 有效期方法
  onClickRight_timeShow(e) {
    let time = e.detail;
    this.setData({
      valid_day: this.timestampToTime(time),
      timeShow: false
    })
  },
  // 时间戳转日期
  timestampToTime: function (timestamp) {
    if (!Number(timestamp) || timestamp == "") {
      return "";
    }
    let date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let Y = date.getFullYear();
    let M = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;

    return `${M}${Y}`;
  },
  onChange(e) {
    let name = e.detail;
    this.setData({
      radioCard: name,
    })
  },
  checkJson() {
    if (app._isTextEmpty(this.data.card_type)) {
      app.tips('请选择银行卡类型')
      return false
    }
    if (app._isTextEmpty(this.data.card)) {
      app.tips('请输入卡号')
      return false
    }
    if (app._isTextEmpty(this.data.mobile)) {
      app.tips('请输入银行卡预留手机号')
      return false
    }
    if (app._isTextEmpty(this.data.name)) {
      app.tips('请输入持卡人姓名')
      return false
    }
    if (app._isTextEmpty(this.data.identity)) {
      app.tips('请输入身份证')
      return false
    }
    if (this.card_type == 2) {
      if (app._isTextEmpty(this.data.valid_day)) {
        app.tips('请选择月份/年份')
        return false
      }
      if (app._isTextEmpty(this.data.cvv2)) {
        app.tips('请选择月份/年份')
        return false
      }
    }
    return true
  },
  // 确认
  confirmBtn() {
    if (!this.checkJson()) {
      return
    }
    let urlStr = app.getNetAddresss("plugin.eplus-pay.frontend.card.bindCardApply");
    let json = {
      card_type: this.data.card_type,
      card_no: this.data.card,
      phone: this.data.mobile,
      name: this.data.name,
      identity_no: this.data.identity
    };
    if (this.data.card_type == 2) {
      // 信用卡多 安全码和有效期
      json.validate = this.data.valid_day;
      json.cvv2 = this.data.cvv2;
    }
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          console.log(res)
          wx.showModal({
            showCancel: false,
            content: res.msg,
            success: () => {
              if (this.data.to == 'SilverBankListwisdom') {
                wx.redirectTo({
                  url: '/packageF/silverPointPay/SilverBankList/SilverBankList',
                })
              } else if (this.data.to == 'wisdomBindTask') {
                wx.redirectTo({
                  url: '/packageF/others/wisdom/wisdomBindTask/wisdomBindTask',
                })
              } else {
                wx.navigateBack({
                  delta: 1,
                })
              }
            }
          })
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})